/* Sentinel - IRC Statistical and Operator Services
** mem.c - Memory management functions and wrappers
**
** Copyright W. Campbell and others.  See README for more details
** Some code Copyright: Jonathan George, Kai Seidler, ircd-hybrid Team,
**                      IRCnet IRCD developers.
**
** $Id: mem.c,v 1.3 2003/12/06 00:27:24 wcampbel Exp $
*/

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "slog.h"
#include "mem.h"

/* our own malloc with logging, sanity checking, and zeroing out memory */
void *smalloc(size_t size)
{
  void *buf;

  if (!size)
  {
    slog(DEFAULT_LOG, L_CRIT, "smalloc(): illegal attempt to allocate 0 bytes!");
    size = 1;
  }
  buf = calloc(1, size);
  if (!buf)
  {
    slog(DEFAULT_LOG, L_ALERT, "smalloc(): out of memory.");
    close_logfiles();
    exit(0);
  }
  return buf;
}

/* our own strdup with logging and sanity checking */
char *sstrdup(const char *s)
{
  char *t;

  if (s == NULL)
  {
    slog(DEFAULT_LOG, L_ALERT, "sstrdup(): attempt to duplicate NULL string.");
    return NULL;
  }
  t = strdup(s);
  if (t == NULL)
  {
    slog(DEFAULT_LOG, L_ALERT, "sstrdup(): out of memory.");
    close_logfiles();
    exit(0);
  }
  return t;
}
